package com.beauty.dressing.module.mine.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.bean.BaseBean;
import com.beauty.dressing.common.bean.LeaseBean;
import com.beauty.dressing.common.bean.User;
import com.beauty.dressing.common.view.LoadingNodataLayout;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.module.mine.adapter.MyLeaseAdapter;
import com.beauty.dressing.module.mine.holder.MyLeaseFooterHolder;
import com.beauty.dressing.module.mine.persenter.MyLeaseControl;
import com.beauty.dressing.module.mine.persenter.MyLeasePresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 退租
 */
public class WithdrawalFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    MyLeaseAdapter myLeaseAdapter;
    SwipeRefreshLayout swiperefresh;
    private LoadingNodataLayout mNodtaLayout;

    @Override
    public void setContentView() {
        setContentView(R.layout.common_recycler_no_title);
    }

    @Override
    public void findView() {
        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        myLeaseAdapter = new MyLeaseAdapter(context);
        mRecyclerView.setAdapter(myLeaseAdapter);
        MyLeaseFooterHolder myLeaseFooterHolder = new MyLeaseFooterHolder(context, LayoutInflater.from(context), mRecyclerView);
        myLeaseAdapter.addFooterView(myLeaseFooterHolder.itemView);
        swiperefresh = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
        swiperefresh.setOnRefreshListener(this);
        mNodtaLayout = (LoadingNodataLayout) findViewById(R.id.nodata_layout);
        mNodtaLayout.setCenterGravity();
    }

    @Override
    public void initData() {
        User user = LBController.getInstance().getCacheManager().getLoginUser();
//        myLeasePresenter.myLease(String.valueOf(user.member_id), "1", user.token, "1");
        myLeasePresenter.myLease("1", "2", "3889f68da98eea80ffaf64ae8cae6ff4347a435c", "1");
        mNodtaLayout.setVisibility(View.VISIBLE);
        mNodtaLayout.setIsLoadingUi(true);

    }

    MyLeasePresenter myLeasePresenter = new MyLeasePresenter(new MyLeaseControl() {

        @Override
        public void onMyLease(boolean isSuccess, int code, String message, List<LeaseBean> list) {
            if (isSuccess) {
                ArrayList<BaseBean> mList = new ArrayList<>();
                mList.addAll(list);
                myLeaseAdapter.setChild(mList);
                mNodtaLayout.setVisibility(View.GONE);
                mNodtaLayout.setIsLoadingUi(false);
                swiperefresh.setRefreshing(false);
            }
        }
    });

    @Override
    public void onRefresh() {
        initData();
    }
}
